import java.util.*;

public class InterviewQuestion10_02 {

    public static void main(String[] args) {
        String[] strs = {"eat", "tea", "tan", "ate", "nat", "bat"};

        System.out.println(groupAnagrams(strs));
    }

    public static List<List<String>> groupAnagrams(String[] strs) {
        Map<Integer, List<String>> map = new HashMap<>();
        for (String ss : strs) {
            char[] cs = ss.toCharArray();
            Arrays.sort(cs);
            int hashcode = Arrays.hashCode(cs);
            List<String> list = map.getOrDefault(hashcode, new ArrayList<>());
            list.add(ss);
            map.put(hashcode, list);
        }
        return new ArrayList<>(map.values());
    }
}
